************************************************************************************
************************************************************************************
*				The causal effect of polls on turnout intention:				   *
*			A local randomization regression discontinuity approach				   *
*                           Brugarolas+Miller; 2020								   *
************************************************************************************
************************************************************************************

/*
net install rdlocrand, from(https://raw.githubusercontent.com/rdpackages/rdlocrand/master/stata) replace

*/
capture log close
*cd"/Users/pablobrugarolas/Google Drive/Cloud/2020-21/RA Luis Miller/data/PA submit/do-data-files/submit"
cd "/Users/luismiguelmillermoya/Dropbox/Papers/Active/CIS/Submission/PA/Final/submit/"
log using "Script/BM_PA.log", replace

set seed 1
clear
set more off

use "Data/BM_PA.dta", clear
set sortseed 1
* Donut hole
drop if z == 0

************************************************************************************
* Figure 1: Window selection: minimum p-value against window length
************************************************************************************

rdwinselect z female a30-a70 urban1 urban2 urban3, stat(hotelling) level(0.05) wstep(0.2) nwindows(13) plot graph_options(graphregion(color(white)) xtitle(Window length (at each side of the cutoff)) ysc(r(.4)) xlab(0.2(0.2)2.6) ytitle(P-value) yline(0.05, lcolor(black) lpattern(dash)) mcolor(black) ylab(, nogrid) note(The dotted line corresponds top-value=.05, pos(1)))

graph export "Results/Figures/Figure 1.pdf", as(pdf) replace

************************************************************************************
* Figure 2: See BM_PA_Figure2.Rmd
************************************************************************************


************************************************************************************
* Confidence Interval for the effect of treatment on page 5 (0.8 window)							
************************************************************************************
 
rdrandinf ivotd z, seed(50) wl(-0.800001) wr(0.800001) ci(0.05)


************************************************************************************
* Table A2: Sentitivity of the results to window choice 												
************************************************************************************

use "Data/BM_PA.dta", clear

* Donut hole
drop if z == 0

* Loop for getting results across the 13 windows

** set up  window size macros 
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

rdrandinf ivotd z, seed(50) wl(`xi') wr(`yi')
}
* 

* Export results from Table A2
*-----------------------------------------------------------------------------------

putexcel set "Results/Tables/Table A2", modify

local Ys 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001

local n : word count `Y'

matrix A = J(13,3,.)

forvalues i=1/`n' {
local yis : word `i' of `Ys'
matrix A[`i',1] = `yis'
}
*
forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: sum ivotd if z >= `xi' & z <= 0
	scalar meanc = r(mean)
	quietly: sum ivotd if z >= 0 & z <= `yi'	
	scalar meant = r(mean)
	matrix A[`i',2] = meanc
	matrix A[`i',3] = meant
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)

matrix B = J(13,4,.)

local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: rdrandinf ivotd z, seed(50) wl(`xi') wr(`yi')
    scalar diff = r(obs_stat)
	scalar fpvalue = r(randpval)
	scalar nleft = r(N_left)
	scalar nright = r(N_right)
	matrix B[`i',1] = diff
	matrix B[`i',2] = fpvalue
	matrix B[`i',3] = nleft
	matrix B[`i',4] = nright
}
*
matrix list B, format(%4.3f) 
putexcel F3=matrix(B)


************************************************************************************
* Table A3: Placebo test: 1st placebo week before Poll1’s publication								
************************************************************************************

use "Data/BM_PA.dta", clear

gen  z_p = 0 if t_inc==33.2
replace  z_p = t_inc - 33.2 if t<19 
replace  z_p = t_inc - 33.2 if t>20 
	
replace z_p = round(z_p, .1)

drop if z_p == 0


* Loop for getting results across the 13 windows
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

rdrandinf ivotd z_p, seed(50) wl(`xi') wr(`yi')
}
*

* Export results from Table A3
*-----------------------------------------------------------------------------------

putexcel set "Results/Tables/Table A3", modify

local Ys 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001

local n : word count `Y'

matrix A = J(13,3,.)

forvalues i=1/`n' {
local yis : word `i' of `Ys'


matrix A[`i',1] = `yis'
}
*
forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: sum ivotd if z_p >= `xi' & z_p <= 0
	scalar meanc = r(mean)
	quietly: sum ivotd if z_p >= 0 & z_p<= `yi'	
	scalar meant = r(mean)
	matrix A[`i',2] = meanc
	matrix A[`i',3] = meant
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)


matrix B = J(13,4,.)

local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly:  rdrandinf ivotd z_p, seed(50) wl(`xi') wr(`yi')
    scalar diff = r(obs_stat)
	scalar fpvalue = r(randpval)
	scalar nleft = r(N_left)
	scalar nright = r(N_right)
	matrix B[`i',1] = diff
	matrix B[`i',2] = fpvalue
	matrix B[`i',3] = nleft
	matrix B[`i',4] = nright
}
*
matrix list B, format(%4.3f) 
putexcel F3=matrix(B)


************************************************************************************
* Table A4: Placebo test: 2nd placebo week before Poll1’s publication								
************************************************************************************

use "Data/BM_PA.dta", clear

gen  z_p2 = 0 if t_inc==26.2
replace  z_p2 = t_inc - 26.2 if t<19 
replace  z_p2 = t_inc - 26.2 if t>20 

replace z_p2 = round(z_p2, .1)

drop if z_p2 == 0

* Loop for getting results across the 13 windows
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

rdrandinf ivotd z_p2, seed(50) wl(`xi') wr(`yi')
}
*

* Export results from Table A4
*-----------------------------------------------------------------------------------

putexcel set "Results/Tables/Table A4", modify

local Ys 0.2 0.4 0.6 0.8 1.0 1.2 1.4 1.6 1.8 2.0 2.2 2.4 2.6
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001

local n : word count `Y'

matrix A = J(13,3,.)

forvalues i=1/`n' {
local yis : word `i' of `Ys'


matrix A[`i',1] = `yis'
}
*
forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: sum ivotd if z_p2 >= `xi' & z_p2 <= 0
	scalar meanc = r(mean)
	quietly: sum ivotd if z_p2 >= 0 & z_p2<= `yi'	
	scalar meant = r(mean)
	matrix A[`i',2] = meanc
	matrix A[`i',3] = meant
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)

matrix B = J(13,4,.)

local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 -2.000001 -2.200001 -2.400001 -2.600001
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly:  rdrandinf ivotd z_p2, seed(50) wl(`xi') wr(`yi')
    scalar diff = r(obs_stat)
	scalar fpvalue = r(randpval)
	scalar nleft = r(N_left)
	scalar nright = r(N_right)
	matrix B[`i',1] = diff
	matrix B[`i',2] = fpvalue
	matrix B[`i',3] = nleft
	matrix B[`i',4] = nright
}
*
matrix list B, format(%4.3f) 
putexcel F3=matrix(B)


************************************************************************************
* Table A5: Placebo test: 3rd placebo week after Poll1’s publication						       		
************************************************************************************

use "Data/BM_PA.dta", clear

gen  z_p_right = 0 if z==1.4
replace  z_p_right = z - 1.4 if t<19 
replace  z_p_right = z - 1.4 if t>20 

replace z_p_right = round(z_p_right, .1)

drop if z_p_right == 0

** set up  the new window size macros 
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 

local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

rdrandinf ivotd z_p_right, seed(50) wl(`xi') wr(`yi')
}
*

* Export results from Table A5
*-----------------------------------------------------------------------------------

putexcel set "Results/Tables/Table A5", modify


local Ys 0.2 0.4 0.6 0.8 1.0 1.2
local Y  0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 

local n : word count `Y'

matrix A = J(7,3,.)

forvalues i=1/`n' {
local yis : word `i' of `Ys'


matrix A[`i',1] = `yis'
}
*
forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: sum ivotd if z_p_right >= `xi' & z_p_right <= 0
	scalar meanc = r(mean)
	quietly: sum ivotd if z_p_right >= 0 & z_p_right<= `yi'	
	scalar meant = r(mean)
	matrix A[`i',2] = meanc
	matrix A[`i',3] = meant
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)


forvalues i=1/`n' {
local yi : word `i' of `Y'
matrix A[`i',1] = `yi'
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)
 
matrix B = J(7,4,.)

local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 
local Y 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 
local n : word count `X'

forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: rdrandinf ivotd z_p_right, seed(50) wl(`xi') wr(`yi')
    scalar diff = r(obs_stat)
	scalar fpvalue = r(randpval)
	scalar nleft = r(N_left)
	scalar nright = r(N_right)
	matrix B[`i',1] = diff
	matrix B[`i',2] = fpvalue
	matrix B[`i',3] = nleft
	matrix B[`i',4] = nright
}
*
matrix list B, format(%4.3f) 
putexcel F3=matrix(B)


************************************************************************************
* Table A6: Density test of the running variable											       		
************************************************************************************

use "Data/BM_PA.dta", clear

drop if z == 0

rdwinselect z, wmin(0.800001) nwindows(1) seed(50)

* Export results from Table A6
*-----------------------------------------------------------------------------------

* Done manually as rdwinselect does not store the needed output


************************************************************************************
* Table A7: Conservative estimation															       		 
************************************************************************************

use "Data/BM_PA.dta", clear

gen inc_con = .     
replace inc_con = 0 if hour==1
replace inc_con = 0.20000000 if hour==2
replace inc_con = 0.40000000 if hour==3
replace inc_con = 0.60000000 if hour==4
replace inc_con = 0.80000000  if hour==5
		
gen t_inc_con = t+inc_con
drop if t_inc_con >39.8 & t_inc_con < 41 
replace t_inc_con=t_inc_con  + 1 if t_inc_con < 41 

gen  z_con = 0 if t_inc_con==41
replace  z_con = t_inc_con - 41 if t<19 
replace  z_con = t_inc_con - 41 if t>20 

replace z_con=z_con  + 0.2 if z_con >=0 
		
replace z_con = round(z_con, .1)
		
** set up  the new window size macros 
local X_ -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 
local Y_ 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 

local n : word count `X_'

forvalues i=1/`n' {
local x_i : word `i' of `X_'
local y_i : word `i' of `Y_'

rdrandinf ivotd z_con, seed(50) wl(`x_i') wr(`y_i')
}
*

* Export results from Table A7
*-----------------------------------------------------------------------------------

putexcel set "Results/Tables/Table A7", modify

local Z 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 2.000001 2.200001 2.400001 2.600001
local n : word count `Z'

matrix A = J(13,8,.)

forvalues i=1/`n' {
local zi : word `i' of `Z'
matrix A[`i',1] = `zi'
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)


local Ys 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001
local Y  0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001
local X -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001

local n : word count `Y'

matrix B = J(9,3,.)

forvalues i=1/`n' {
local yis : word `i' of `Ys'
matrix B[`i',1] = `yis'
}
*
forvalues i=1/`n' {
local xi : word `i' of `X'
local yi : word `i' of `Y'

quietly: sum ivotd if z_con >= `xi' & z_con <= 0
	scalar meanc = r(mean)
	quietly: sum ivotd if z_con >= 0 & z_con<= `yi'	
	scalar meant = r(mean)
	matrix B[`i',2] = meanc
	matrix B[`i',3] = meant
}
*
matrix list B, format(%4.3f)
putexcel D7=matrix(B)


matrix C = J(9,4,.)

local X_ -0.200001 -0.400001 -0.600001 -0.800001 -1.000001 -1.200001 -1.400001 -1.600001 -1.800001 
local Y_ 0.200001 0.400001 0.600001 0.800001 1.000001 1.200001 1.400001 1.600001 1.800001 

local n : word count `X_'

forvalues i=1/`n' {
local x_i : word `i' of `X_'
local y_i : word `i' of `Y_'

quietly: rdrandinf ivotd z_con, seed(50) wl(`x_i') wr(`y_i')
    scalar diff = r(obs_stat)
	scalar fpvalue = r(randpval)
	scalar nleft = r(N_left)
	scalar nright = r(N_right)
	matrix C[`i',1] = diff
	matrix C[`i',2] = fpvalue
	matrix C[`i',3] = nleft
	matrix C[`i',4] = nright
}
*

matrix list C, format(%4.3f) 
putexcel G7=matrix(C)

set more off

************************************************************************************************
* Table A8: Representativeness of the sample: comparing the sample we use with the total sample 		
************************************************************************************************

use "Data/BM_PA.dta", clear

* Panel A: 0.8 window

preserve

drop if z == 0
drop if z <= -0.800001
drop if z >= 0.800001 
gen sample = 1
save "Data/BM_PA_0_8w.dta", replace

use "Data/BM_PA.dta", clear

append  using "Data/BM_PA_0_8w"
replace sample = 0 if sample != 1

local vars female a30 a40 a50 a60 a70 a80 urban1 urban2 urban3
foreach var of local vars {
    ttest `var' if  ivotd !=., by(sample)
}
*
restore

* Panel B: 2.6 window

preserve
drop if z == 0
drop if z <= -2.600001
drop if z >= 2.600001 
gen sample = 1
save "Data/BM_PA_2_6w.dta", replace

use "Data/BM_PA.dta", clear

append  using "Data/BM_PA_2_6w"
replace sample = 0 if sample != 1

local vars female a30 a40 a50 a60 a70 a80 urban1 urban2 urban3
foreach var of local vars {
    ttest `var' if  ivotd !=., by(sample)
}
*
restore

* Export results from Table A8
*-----------------------------------------------------------------------------------

** Panel A: 0.8 window

use "Data/BM_PA.dta", clear

append  using "Data/BM_PA_0_8w"
replace sample = 0 if sample != 1


putexcel set "Results/Tables/Table A8", modify

matrix A = J(10,6,.) 

local i = 1
local vars female a30 a40 a50 a60 a70 a80 urban1 urban2 urban3
foreach var of local vars {
	tempname `var'
    quietly: ttest `var' if  ivotd !=., by(sample)
	scalar meantotal``var''= r(mu_1)
	scalar meanrestricted``var'' = r(mu_2)
	scalar pvalue``var'' = r(p)
	scalar ntotal``var'' = r(N_1)
	scalar nrestricted``var'' = r(N_2)
	matrix A[`i',1] = meantotal``var''
	matrix A[`i',2] = meanrestricted``var''
	matrix A[`i',3] = meantotal``var'' - meanrestricted``var''
	matrix A[`i',4] = pvalue``var''
	matrix A[`i',5] = ntotal``var''
	matrix A[`i',6] = nrestricted``var'' 
	local ++i
}
*
matrix list A, format(%4.3f)
putexcel C3=matrix(A)

** Panel B: 2.6 window

use "Data/BM_PA.dta", clear

append  using "Data/BM_PA_2_6w"
replace sample = 0 if sample != 1


putexcel set "Results/Tables/Table A8", modify

matrix A = J(10,6,.) 


local i = 1
local vars female a30 a40 a50 a60 a70 a80 urban1 urban2 urban3
foreach var of local vars {
	tempname `var'
    quietly: ttest `var' if  ivotd !=., by(sample)
	scalar meantotal``var''= r(mu_1)
	scalar meanrestricted``var'' = r(mu_2)
	scalar pvalue``var'' = r(p)
	scalar ntotal``var'' = r(N_1)
	scalar nrestricted``var'' = r(N_2)
	matrix A[`i',1] = meantotal``var''
	matrix A[`i',2] = meanrestricted``var''
	matrix A[`i',4] = pvalue``var''
	matrix A[`i',3] = meantotal``var'' - meanrestricted``var''
	matrix A[`i',5] = ntotal``var''
	matrix A[`i',6] = nrestricted``var'' 
	local ++i
}
*
matrix list A, format(%4.3f)
putexcel C15=matrix(A)


************************************************************************************
* Table A9: The effect of Poll1’ on vote choice: 0.8 days window
************************************************************************************

use "Data/BM_PA.dta", clear

* Donut hole
drop if z == 0

foreach var of varlist ivotopsoe ivotopp ivotocs ivotops ivotovox{
rdrandinf `var' z, seed(50) wl(-0.800001) wr(0.800001)
}
*

* Export results from Table A9
*-----------------------------------------------------------------------------------

putexcel set "Results/Tables/Table A9", modify

matrix A = J(5,6,.) 

local i = 1

foreach var of varlist ivotopsoe ivotopp ivotocs ivotops ivotovox{
	tempname `var'
	quietly: sum `var' if z >= -0.800001 & z <= 0
	scalar meanc``var'' = r(mean)
	quietly: sum `var' if z >= 0 & z <= 0.800001 
	scalar meant``var'' = r(mean)
	quietly: rdrandinf `var' z, seed(50) wl(-0.800001) wr(0.800001)
	scalar diff``var'' = r(obs_stat)
	scalar fpvalue``var'' = r(randpval)
	scalar nleft``var'' = r(N_left)
	scalar nright``var'' = r(N_right)
	matrix A[`i',1] = meanc``var''
	matrix A[`i',2] = meant``var''
	matrix A[`i',3] = diff``var''
	matrix A[`i',4] = fpvalue``var''
	matrix A[`i',5] = nleft``var''
	matrix A[`i',6] = nright``var''	
	local ++i
}
*

matrix list A, format(%4.3f)
putexcel C3=matrix(A)

log close

************************************************************************************
************************************************************************************
************************************************************************************
